﻿'use strict'

var hogwarts = {};

hogwarts.roster = [];

hogwarts.displayRoster = function (index) {
    var request = new XMLHttpRequest();
    request.open('GET', 'https://hogwartsroster.firebaseio.com/.json');
    request.onload = function () {
        var data = JSON.parse(this.response);
        for (var i in data) {
            data[i].id = i;
            hogwarts.roster.push(data[i]);
        }
        var h = '<table class="table table-bordered table-striped">';
        h += '<tr>';
        h += '<th>Student Name</th>';
        h += '<th>House</th>';
        h += '<th>Year</th>';
        h += '<th></th>';
        h += '<th></th>';
        h += '</tr>';

        for (var index in hogwarts.roster) {

            var student = hogwarts.roster[index];

            h += '<tr>';
            h += '<td>' + student.name + '</td>';
            h += '<td>' + student.house + '</td>';
            h += '<td>' + student.year + '</td>';
            h += '<td><button onclick="hogwarts.updateRecord(' + index + ')">Update</button>';
            h += '<td><button onclick="hogwarts.expelStudent(' + index + ')">Expelled</button>';
            h += '</tr>';
        }

        h += '</table>';

        document.getElementById('table').innerHTML = h;
    }
    request.send();
}

hogwarts.addStudent = function () {
    var name = document.getElementById('name');
    var house = document.getElementById('house');
    var year = document.getElementById('year');

    var newStudent = {
        name: name.value,
        house: house.value,
        year: year.value
    };

    var request = new XMLHttpRequest();
    request.open('POST', 'https://hogwartsroster.firebaseio.com/.json');
    request.onload = function () {
        var data = JSON.parse(this.response);
        newStudent.id = data.name;

        hogwarts.roster.push(newStudent);

        name.value = house.value = year.value = ' ';

        hogwarts.displayRoster();
    }

    var newStudentAsStr = JSON.stringify(newStudent);
    request.send(newStudentAsStr);
   
}

hogwarts.expelStudent = function (index) {
    var student = this.roster[index];

    if(confirm('Are you sure?')) {
        var request = new XMLHttpRequest();
        request.open('DELETE', 'https://hogwartsroster.firebaseio.com/' + student.id + '.json');
        request.send();

        hogwarts.roster.splice(index, 1);

        document.getElementById('table').innerHTML = " ";
        hogwarts.displayRoster(index);
    }
}

hogwarts.updateRecord = function (index) {
    var student = this.roster[index];

        var name = document.getElementById('name');
        var house = document.getElementById('house');
        var year = document.getElementById('year');
               
        name.value = student.name;
        house.value = student.house;
        year.value = student.year;

        document.getElementById('addStudent').style.visibility = 'hidden';
        document.getElementById('editStudent').style.visibility = 'visible';
}

hogwarts.submitEdits = function (index) {
    var student = this.roster[index];

    var name = document.getElementById('name');
    var house = document.getElementById('house');
    var year = document.getElementById('year');

    var updatedStudent = {
        name: name.value,
        house: house.value,
        year: year.value
    };

    var request = new XMLHttpRequest();
    request.open('PUT', 'https://hogwartsroster.firebaseio.com/' + student.id + '.json');
    request.onload = function () {
        var data = JSON.parse(this.response);

        hogwarts.roster.splice(index, 1);

        hogwarts.roster.push(updatedStudent);

        name.value = house.value = year.value = ' ';

        hogwarts.displayRoster();
    }
    var upStudentAsStr = JSON.stringify(updatedStudent);
    request.send(upStudentAsStr);

    document.getElementById('addStudent').style.visibility = 'visible';
    document.getElementById('editStudent').style.visibility = 'hidden';

document.getElementById('table').innerHTML = " ";
this.displayRoster(index);
}

hogwarts.displayRoster();